<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button
              :loading="loading"
              class="login_btn"
              type="primary"
              @click="login"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { User, Lock } from "@element-plus/icons-vue";
import { useRouter, useRoute } from "vue-router";
import { reactive, ref } from "vue";
import { ElNotification } from "element-plus";
import { getTime } from "@/utils/time";

// 获取路由器
let $router = useRouter();
// 路由对象
let $route = useRoute();

console.log(User);

// 定义变量按钮加载效果
let loading = ref(false);

// 获取el-form组件
let loginForms = ref();

// 引入用户相关的小仓库
import useUserStore from "@/store/modules/user";
let useStore = useUserStore();

//收集账号喝密码的数据
let loginForm = reactive({
  username: "admin",
  password: "atguigu123",
});
// 自定义校验规则
const validatorUserName = (rule: any, value: any, callback: any) => {
  // rule:既为校验规则对象
  // value:既为表单元素文本内容
  // 函数:如果条件符合callback放行通过即为
  // 如果不符合条件callBack方法,注入错误提示信息
  if (value.length >= 5) {
    callback();
  } else {
    callback(new Error("账号长度至少为5位"));
  }
};

const validatorPassword = (rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback();
  } else {
    callback(new Error("账号长度至少为6位"));
  }
};

// 定义表单校验规则
const rules = {
  username: [
    // {required:true,message:'用户名不能为空',trigger:'blur'},
    // {required:true,min:6,max:10,message:'账号长度至少为6位',trigger:'change'}
    { trigger: "change", validator: validatorUserName },
  ],

  password: [
    // {required:true,min:6,max:15,message:'密码长度至少为6位',trigger:'change'}
    { trigger: "change", validator: validatorPassword },
  ],
};

// 登录按钮的回调
const login = async () => {
  // 保证全部的表单项校验通过再发请求
  await loginForms.value.validate();
  // 加载效果
  loading.value = true;
  try {
    await useStore.userLogin(loginForm);

    // 判断登录的时候,路由路径当中是否有query参数,如果有query参数就跳query参数路径,没有就跳首页
    let redirect: any = $route.query.redirect;
    $router.push({ path: redirect || "/" });
    // 登录成功提示信息
    ElNotification({
      type: "success",
      message: "欢迎回来",
      title: `HI,${getTime()}好`,
    });
  } catch (error) {
    loading.value = false;
    // 登录失败的提示信息
    ElNotification({
      type: "error",
      message: (error as Error).message,
    });
    // 登录成功加载效果消失
    loading.value = false;
  }
};
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url("@/assets/images/background.jpg") no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url("@/assets/images/login_form.png") no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      font-size: 40px;
      color: white;
    }
    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0px;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
